/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/27
 * Time: 17:15
 */
$(function(){

    sendAjax('/v1/manager/rent/detail.json', 'get', {id:id}, function(json){
        var rent = json.body.data;
        if (rent) {
            $('.apart-name').text(rent.apart_name);
            $('.apart-id').val(rent.apart_id);
            $('.rent-user').text(rent.realname);
            $('.user-id').val(rent.user_id);
            $('.start-end-time').val(rent.rent_time);
            $('.rent-fee').val(rent.fee);
            $('.rent-remark').val(rent.remark);
        }
    });

    $('.btn-save').on('click', function(){
        var data = {
            id: $('.rent-id').val(),
            apart_id: $('.apart-id').val(),
            user_id: $('.user-id').val(),
            fee: $('.rent-fee').val(),
            remark: $('.rent-remark').val(),
            start_end_time: $('.start-end-time').val()
        };
        if (checkForm(data)){
            sendAjax('/v1/manager/update/rent', 'post', {data:data}, function(json){
                $('#action-tip').find('.tip-content').text(json.message);
                $('#action-tip').modal('show');
            });
        }
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.href = '/manager/rent.html';
    });
});

function checkForm(data) {
    if (!data.id) {
        $('#action-tip .modal-body .tip-content').text('非法请求');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.apart_id) {
        $('#action-tip .modal-body .tip-content').text('请选择房源');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.user_id) {
        $('#action-tip .modal-body .tip-content').text('非法请求');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.fee) {
        $('#action-tip .modal-body .tip-content').text('请填写月租金');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.start_end_time) {
        $('#action-tip .modal-body .tip-content').text('请填写起止时间');
        $('#action-tip').modal('show');
        return false;
    }
    if (data.start_end_time.split(' ~ ').length < 2) {
        $('#action-tip .modal-body .tip-content').text('请选择起止时间');
        $('#action-tip').modal('show');
        return false;
    }
    if (new Date(data.start_end_time.split(' ~ ')[1]) < new Date(data.start_end_time.split(' ~ ')[0])) {
        $('#action-tip .modal-body .tip-content').text('开始时间不能大于结束时间');
        $('#action-tip').modal('show');
        return false;
    }
    return true;
}